#include	"XmlParser.hpp"
#include	"tinyxml/tinyxml.h"

using namespace std;
extern zia::api::DataTree			confEnv;

XmlParser::XmlParser()
{
	this->_File = "";
}

XmlParser::XmlParser(const std::string &file)
{
	this->_File = file;
}

XmlParser::~XmlParser()
{
}

int							XmlParser::parseFile()
{
	int		ret = -2;

	if (this->getFile().size() != 0)
	{
		TiXmlDocument		doc("conf.xml");

		if (!doc.LoadFile())
		{
			this->resetFile();
			ret = -1;
		}
		else
		{
			try
			{
				TiXmlElement *elem = doc.FirstChildElement();
				ret = 1;
				if ((elem->Value() != NULL) && (strcmp(elem->Value(), "Module") == 0))
				{
					TiXmlElement *child = elem->FirstChildElement();
					if (child && !strcmp(child->Value(), "PHP") && child->Attribute("state"))
					{
#ifndef _WIN32
						if (child->Attribute("state") == string("ENABLE") || child->Attribute("state") == string("DISABLE"))
#else
						if (strcmp(child->Attribute("state"), "ENABLE") == 0 || strcmp(child->Attribute("state"), "DISABLE") == 0)
#endif
						{
							confEnv["PHP"] = child->Attribute("state");
						}
						else
						{
							throw("in php, for state's field, only ENABLE or DISABLE values are accepted");
						}
						child = child->NextSiblingElement();
						if (child && !strcmp(child->Value(), "SSL") && child->Attribute("state"))
						{
#ifndef	_WIN32
						  if (child->Attribute("state") == string("ENABLE") || child->Attribute("state") == string("DISABLE"))
#else
						if (strcmp(child->Attribute("state"), "ENABLE") == 0 || strcmp(child->Attribute("state"), "DISABLE") == 0)
#endif
								confEnv["SSL"] = child->Attribute("state");
							else
								throw("in ssl,for state's field, only ENABLE or DISABLE values are accepted");
						}
						else
							throw("SSL should be after PHP");
					}
					else
						throw("PHP should be the first child of Module");
				}
				else
					throw("<Module> wasn't found, or isn't the root node");
			}
			catch (const char * err)
			{
				cerr << "An error has occured: " << err << endl;
				cerr << "Regenerating conf file with default values" << endl;
				this->resetFile();
				cerr << "Done" << endl;
				confEnv["PHP"] = "DISABLE";
				confEnv["SSL"] = "DISABLE";
			}
			catch (...)
			{
				cerr << "Unknown Error has occured" << endl;
				cerr << "Regenerating conf file with default values" << endl;
				this->resetFile();
				cerr << "Done" << endl;
				confEnv["PHP"] = string("DISABLE");
				confEnv["SSL"] = string("DISABLE");
			}
		}
	}
	return (ret);
}

void						XmlParser::setFile(const std::string &file)
{
	this->_File = file;
	return ;
}

const std::string &			XmlParser::getFile()
{
	return (this->_File);
}

void						XmlParser::resetFile()
{
	ofstream				outfile;
	ostringstream			newfile;

	newfile << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl;
	newfile << "<Module>" << endl;
	newfile << "<PHP state=\"DISABLE\"/>" << endl;
	newfile << "<SSL state=\"DISABLE\"/>" << endl;
	newfile << "</Module>" << endl;

	outfile.open("conf.xml");
	outfile.write("", 0);
	outfile.close();
	outfile.open("conf.xml");
	outfile.write(newfile.str().c_str(), newfile.str().size());
	outfile.close();
}